package com.chujian.system.auth.service.impl;

import com.chujian.framework.auth.Accessor;
import com.chujian.framework.auth.token.BaseTokenService;
import com.chujian.sys.entity.SysUser;
import com.chujian.system.auth.service.ITokenService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class TokenService extends BaseTokenService implements ITokenService {

    /**
     * 过期时间,单位s,默认一天
     */
    @Value("${chujian.auth.token.expire:86400}")
    private Long expire;
    /**
     * 刷新token过期时间,单位s，默认一周
     */
    @Value("${chujian.auth.token.refreshExpire:604800}")
    private Long refreshExpire;

    @Override
    public String genToken(Long prodId, Long platId, SysUser user) throws Exception {
        Accessor accessor = creAccessor(prodId, platId, user);
        return creToken(accessor, expire);
    }

    @Override
    public String genRefreshToken(Long prodId, Long platId, SysUser user) throws Exception {
        Accessor accessor = creAccessor(prodId, platId, user);
        return creToken(accessor, refreshExpire);
    }

    private Accessor creAccessor(Long prodId, Long platId, SysUser user) {
        return new Accessor()
                .setUserId(user.getUserId())
                .setRealName(user.getUserName())
                .setProdId(prodId)
                .setPlatId(platId)
                .setAcId(user.getAcId())
                .setVersion("1.0")
                .setCompId(user.getLastLoginCompId());
    }

}
